/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.excel.convert;

import cn.hutool.core.util.ArrayUtil;
import cn.star.framework.excel.core.Column;
import java.util.HashSet;
import java.util.Set;

/**
 * 支持{@link Column}自动挂载的属性<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public interface ColumnAutoMount {

  /**
   * 支持的{@link Column#type}
   *
   * @return
   */
  Class<?>[] getSupportTypes();

  /**
   * 类型的直接父类及其实现的接口(非递归)
   *
   * @param type 类型
   * @return
   */
  default Set<String> getTypeSuperClasses(Class<?> type) {
    Set<String> types = new HashSet<>();
    // 当前类
    types.add(type.getName());

    // 直接父类
    Class<?> superClass = type.getSuperclass();
    if (superClass != null) {
      types.add(superClass.getName());
    }
    // 接口
    Class<?>[] interfaces = type.getInterfaces();
    if (ArrayUtil.isNotEmpty(interfaces)) {
      for (Class<?> clazz : interfaces) {
        if (clazz != null) {
          types.add(clazz.getName());
        }
      }
    }

    return types;
  }

  /**
   * 判断一个类型是否被支持
   *
   * @param type 类型
   * @return
   */
  default boolean isSupportType(Class<?> type) {
    Class<?>[] types = this.getSupportTypes();
    if (ArrayUtil.isNotEmpty(types)) {
      Set<String> supportTypeNames = this.getTypeSuperClasses(type);
      for (Class<?> aClass : types) {
        if (supportTypeNames.contains(aClass.getName())) {
          return true;
        }
      }
    }
    return false;
  }
}
